package com.ruoyi.dict.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dict.mapper.TDictEquipmentTypeMapper;
import com.ruoyi.dict.domain.TDictEquipmentType;
import com.ruoyi.dict.service.ITDictEquipmentTypeService;
import com.ruoyi.dict.service.IDictUsageCheckService;
import com.ruoyi.common.exception.ServiceException;

/**
 * 设备类型Service业务层处�?
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class TDictEquipmentTypeServiceImpl implements ITDictEquipmentTypeService 
{
    @Autowired
    private TDictEquipmentTypeMapper tDictEquipmentTypeMapper;
    
    @Autowired
    private IDictUsageCheckService dictUsageCheckService;

    /**
     * 查询设备类型
     * 
     * @param code 设备类型主键
     * @return 设备类型
     */
    @Override
    public TDictEquipmentType selectTDictEquipmentTypeByCode(String code)
    {
        return tDictEquipmentTypeMapper.selectTDictEquipmentTypeByCode(code);
    }

    /**
     * 查询设备类型列表
     * 
     * @param tDictEquipmentType 设备类型
     * @return 设备类型
     */
    @Override
    public List<TDictEquipmentType> selectTDictEquipmentTypeList(TDictEquipmentType tDictEquipmentType)
    {
        return tDictEquipmentTypeMapper.selectTDictEquipmentTypeList(tDictEquipmentType);
    }

    /**
     * 新增设备类型
     * 
     * @param tDictEquipmentType 设备类型
     * @return 结果
     */
    @Override
    public int insertTDictEquipmentType(TDictEquipmentType tDictEquipmentType)
    {
        return tDictEquipmentTypeMapper.insertTDictEquipmentType(tDictEquipmentType);
    }

    /**
     * 修改设备类型
     * 
     * @param tDictEquipmentType 设备类型
     * @return 结果
     */
    @Override
    public int updateTDictEquipmentType(TDictEquipmentType tDictEquipmentType)
    {
        return tDictEquipmentTypeMapper.updateTDictEquipmentType(tDictEquipmentType);
    }

    /**
     * 批量删除设备类型
     * 
     * @param codes 需要删除的设备类型主键
     * @return 结果
     */
    @Override
    public int deleteTDictEquipmentTypeByCodes(String[] codes)
    {
        // 检查每个设备类型是否被使用
        for (String code : codes) {
            if (dictUsageCheckService.isEquipmentTypeInUse(code)) {
                String description = dictUsageCheckService.getEquipmentTypeUsageDescription(code);
                throw new ServiceException(description);
            }
        }
        return tDictEquipmentTypeMapper.deleteTDictEquipmentTypeByCodes(codes);
    }

    /**
     * 删除设备类型信息
     * 
     * @param code 设备类型主键
     * @return 结果
     */
    @Override
    public int deleteTDictEquipmentTypeByCode(String code)
    {
        // 检查设备类型是否被使用
        if (dictUsageCheckService.isEquipmentTypeInUse(code)) {
            String description = dictUsageCheckService.getEquipmentTypeUsageDescription(code);
            throw new ServiceException(description);
        }
        return tDictEquipmentTypeMapper.deleteTDictEquipmentTypeByCode(code);
    }
}
